package com.weiyuwqj.app.mobile;

import com.weiyuwqj.api.constant.ItemStatus;
import com.weiyuwqj.api.model.BasItem;
import com.weiyuwqj.api.model.SysArticle;
import com.weiyuwqj.api.model.vo.BasItemVo;
import com.weiyuwqj.api.service.BasItemService;
import com.weiyuwqj.api.service.SysArticleService;
import com.weiyuwqj.api.service.SysPictureService;
import com.weiyuwqj.api.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by zhangyong on 2016/9/11.
 */
@Controller
public class MobileIndexController {

    @Autowired
    private BasItemService itemService;

    @Autowired
    private SysPictureService pictureService;

    @Autowired
    private SysArticleService articleService;

    @ResponseBody
    @RequestMapping(value="/mobile/index", method= RequestMethod.POST)
    public Object index(){

        BasItem newItem = itemService.getIsNewItem();
        List<BasItemVo> recomendList=itemService.queryRecomendList(); // 推荐列表
        List<SysArticle> article = articleService.getArticleIndex(); // 最新公告
        Map<String,Object> map=new HashMap<String,Object>(); //返回结果
        map.put("article", article); // 公告
        map.put("newItem",newItem); // 新手标
        map.put("recomendList",recomendList); // 推荐列表
        return map;
    }

    //倒计时结束代码
    @ResponseBody
    @RequestMapping(value="/mobile/index/modifyStatus", method= RequestMethod.POST)
    public void modifyStatus(Integer itemId){
        Integer id=itemId;
        BasItem item = itemService.selectByKey(id);
        Date date = new Date();
        long a = 0L;
        if (item.getReleaseTime() != null) {
            a = item.getReleaseTime().getTime();
        }
        long b = date.getTime();
        long d = a - b;
        if (d <= 0) {
            itemService.updateItemStatus(ItemStatus.WAITOPEN, ItemStatus.OPEN, itemId);
        }
    }


    //获取弹屏图片
    @ResponseBody
    @RequestMapping(value="/mobile/index/getScreenPic", method= RequestMethod.POST)
    public Object getScreenPic(){
        return pictureService.getScreenPicture();
    }

    //获取系统服务器时间
    @ResponseBody
    @RequestMapping(value="/mobile/index/date", method= RequestMethod.POST)
    public Object date(){
        Date date=new Date();
        Long time=date.getTime();
        Map map=new HashMap();
        map.put("time",time);
        return map;
    }
}
